package com.ruoyi.training.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.training.domain.*;
import com.ruoyi.training.dto.ClassStudentDto;
import com.ruoyi.training.service.*;
import com.ruoyi.training.util.xuehua.SnowflakeIdUtils;
import com.ruoyi.training.vo.StudentTrainingQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.training.mapper.PracticeClassMapper;

/**
 * 实践预约班级中间Service业务层处理
 *
 * @author WorrilessGo
 * @date 2025-10-14
 */
@Service
public class PracticeClassServiceImpl implements IPracticeClassService {
    private static final SnowflakeIdUtils snowflake = new SnowflakeIdUtils(5, 8);

    @Autowired
    private PracticeClassMapper practiceClassMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private IProjectsService projectsService;

//    @Autowired
//    private ITrainingClassService trainingClassService;
//
//    @Autowired
//    private IClassStudentService classStudentService;
//
//    @Autowired
//    private IStudentsService studentsService;
//
//    @Autowired
//    private IPracticeClassStudentService practiceClassStudentService;

    /**
     * 查询实践预约班级中间
     *
     * @param practiceClassId 实践预约班级中间主键
     * @return 实践预约班级中间
     */
    @Override
    public PracticeClass selectPracticeClassByPracticeClassId(String practiceClassId) {
        return practiceClassMapper.selectPracticeClassByPracticeClassId(practiceClassId);
    }

    /**
     * 查询实践预约班级中间列表
     *
     * @param practiceClass 实践预约班级中间
     * @return 实践预约班级中间
     */
    @Override
    public List<PracticeClass> selectPracticeClassList(PracticeClass practiceClass) {
        return practiceClassMapper.selectPracticeClassList(practiceClass);
    }

    /**
     * 新增实践预约班级中间
     *
     * @param practiceClass 实践预约班级中间
     * @return 结果
     */
    @Override
    public int insertPracticeClass(PracticeClass practiceClass) {
        practiceClass.setPracticeClassId(String.valueOf(snowflake.nextId()));
        practiceClass.setCreateTime(DateUtils.getNowDate());
        return practiceClassMapper.insertPracticeClass(practiceClass);
    }

    /**
     * 修改实践预约班级中间
     *
     * @param practiceClass 实践预约班级中间
     * @return 结果
     */
    @Override
    public int updatePracticeClass(PracticeClass practiceClass) {
        practiceClass.setUpdateTime(DateUtils.getNowDate());
        return practiceClassMapper.updatePracticeClass(practiceClass);
    }

    /**
     * 批量删除实践预约班级中间
     *
     * @param practiceClassIds 需要删除的实践预约班级中间主键
     * @return 结果
     */
    @Override
    public int deletePracticeClassByPracticeClassIds(String[] practiceClassIds) {
        return practiceClassMapper.deletePracticeClassByPracticeClassIds(practiceClassIds);
    }

    /**
     * 删除实践预约班级中间信息
     *
     * @param practiceClassId 实践预约班级中间主键
     * @return 结果
     */
    @Override
    public int deletePracticeClassByPracticeClassId(String practiceClassId) {
        return practiceClassMapper.deletePracticeClassByPracticeClassId(practiceClassId);
    }

//    /**
//     * 添加预约时，查询当前实践分配班级中的学员
//     * lqx
//     */
//    @Override
//    public List<ClassStudentDto> selectStudentsWhenAdd(StudentTrainingQueryVo studentTrainingQueryVo) {
//        List<ClassStudentDto> classStudentDtoList = new ArrayList<>();
//        //1.通过实践预约id查询出关联的班级id
//        PracticeClass practiceClass = new PracticeClass();
//        practiceClass.setPracticeAppointmentId(studentTrainingQueryVo.getPracticeAppointmentId());
//        List<PracticeClass> practiceClassList = practiceClassMapper.selectPracticeClassList(practiceClass);
//        for (PracticeClass practiceClass1 : practiceClassList) {
//            //获取班级id查询出对应的班级信息
//            TrainClassVo trainClassVo = trainingClassService.selectTrainingClassVoByTrainingClassId(practiceClass1.getTrainingClassId());
//            //通过班级id去班级学员表中查询出对应班级中的学员id
//            ClassStudent classStudent = new ClassStudent();
//            classStudent.setTrainingClassId(practiceClass1.getTrainingClassId());
//            List<ClassStudent> classStudentList = classStudentService.selectClassStudentList(classStudent);
//            //循环学员id封装信息
//            for (ClassStudent classStudent1 : classStudentList) {
//                ClassStudentDto classStudentDto = new ClassStudentDto();
//                Students students = studentsService.selectStudentsByStudentId(classStudent1.getStudentId());
//                classStudentDto.setName(students.getStudentName());//姓名
//                classStudentDto.setIdNumber(students.getIdNumber());//证件编号
//                classStudentDto.setPhone(students.getPhonenumber());//电话
//                classStudentDto.setTrainingProject(trainClassVo.getTrainingProjectName());//培训项目
//                classStudentDto.setTrainingClassCode(trainClassVo.getTrainingClassCode());//培训班编号
//                classStudentDto.setTrainingClassName(trainClassVo.getTrainingClassName());//培训班名称
//                classStudentDto.setHeadTeacher1Name(trainClassVo.getHeadTeacher1Name());//班主任1
//                classStudentDto.setHeadTeacher2Name(trainClassVo.getHeadTeacher2Name());//班主任2
//                //设置考试状态
//                if (classStudent1.getExamStatus() != "" && classStudent1.getExamStatus() != null){
//                    if ("0".equals(classStudent1.getExamStatus())){
//                        classStudentDto.setExamStatus("未排考");
//                    }
//                    if ("1".equals(classStudent1.getExamStatus())){
//                        classStudentDto.setExamStatus("已排考");
//                    }
//                }
//                //通过学员id、班级id、实践预约表id查询出实践状态
//                PracticeClassStudent practiceClassStudent = new PracticeClassStudent();
//                practiceClassStudent.setPracticeAppointmentId(studentTrainingQueryVo.getPracticeAppointmentId());
//                practiceClassStudent.setStudentId(students.getStudentId());
//                practiceClassStudent.setTrainingClassId(practiceClass1.getTrainingClassId());
////                List<PracticeClassStudent> practiceClassStudentList = practiceClassStudentService.selectPracticeClassStudentList(practiceClassStudent);
////                PracticeClassStudent practiceClassStudent1 = practiceClassStudentList.get(0);
//                List<PracticeClassStudent> practiceClassStudentList = practiceClassStudentService.selectPracticeClassStudentList(practiceClassStudent);
//                if (CollectionUtils.isNotEmpty(practiceClassStudentList)) { // 工具类判断非空
//                    PracticeClassStudent practiceClassStudent1 = practiceClassStudentList.get(0);
//                    // 处理实践状态
//                    if (practiceClassStudent1.getPracticeStatus() != "" && practiceClassStudent1.getPracticeStatus() != null) {
//                        if ("0".equals(practiceClassStudent1.getPracticeStatus())) {
//                            classStudentDto.setPracticeStatus("未实践");
//                        }
//                        if ("1".equals(practiceClassStudent1.getPracticeStatus())) {
//                            classStudentDto.setPracticeStatus("已实践");
//                        }
//                    }
//                } else {
//                    // 无实践记录时的默认处理，例如：
//                    classStudentDto.setPracticeStatus("未实践"); // 或根据业务定义默认值
//                }
//
//                classStudentDtoList.add(classStudentDto);
//            }
//        }
//        return classStudentDtoList;
//    }
    /**
     * 添加预约时，查询当前实践分配班级中的学员
     * lqx
     */
    @Override
    public List<ClassStudentDto> selectStudentsWhenAdd(StudentTrainingQueryVo studentTrainingQueryVo) {
        System.out.println(studentTrainingQueryVo.getPracticeAppointmentId());
        // 1. 校验必传参数
        if (studentTrainingQueryVo == null || StringUtils.isEmpty(studentTrainingQueryVo.getPracticeAppointmentId())) {
            return new ArrayList<>();
        }
        // 2. 一次联表查询获取所有符合条件的数据
        List<ClassStudentDto> classStudentDtoList1 = practiceClassMapper.selectAllByCondition(studentTrainingQueryVo);
        System.out.println("classStudentDtoList1 = " + classStudentDtoList1);
        if (CollectionUtils.isEmpty(classStudentDtoList1)) {
            return new ArrayList<>();
        }
        // 3. 状态赋值
        List<ClassStudentDto> resultList = new ArrayList<>();
        for (ClassStudentDto classStudentDto : classStudentDtoList1) {
            //获取培训项目
            if (classStudentDto.getTrainingProjectId() != null) {
                String trainingProject = classStudentDto.getTrainingProjectId();
                Projects projects = projectsService.selectProjectsByProjectId(trainingProject);
                classStudentDto.setTrainingProjectName(projects.getProjectName());
            }
            //获取班主任1
            if (classStudentDto.getHeadTeacher1Id() != null){
                String headTeacher1 = classStudentDto.getHeadTeacher1Id();
                R<SysUser> userInfoById = remoteUserService.getUserInfoById(Long.parseLong(headTeacher1), SecurityConstants.INNER);
                classStudentDto.setHeadTeacher1Name(userInfoById.getData().getNickName());
            }
            //获取班主任2
            if (classStudentDto.getHeadTeacher2Id() != null){
                String headTeacher2 = classStudentDto.getHeadTeacher2Id();
                R<SysUser> userInfoById = remoteUserService.getUserInfoById(Long.parseLong(headTeacher2), SecurityConstants.INNER);
                classStudentDto.setHeadTeacher2Name(userInfoById.getData().getNickName());
            }
            // 考试状态转换（0→未排考，1→已排考）
            String examStatus = classStudentDto.getExamStatus();
            classStudentDto.setExamStatusName("0".equals(examStatus) ? "未排考" : "已排考");

            // 实践状态转换（0→未实践，1→已实践；默认未实践）
            String practiceStatus = classStudentDto.getPracticeStatus();
            classStudentDto.setPracticeStatusName(StringUtils.hasText(practiceStatus) ?
                    ("0".equals(practiceStatus) ? "未实践" : "已实践") : "未实践");
            resultList.add(classStudentDto);
        }
        return resultList;
    }

    /**
     * 通过实践预约表id删除对应内容
     * */
    @Override
    public void deletePracticeClassByPracticeAppointmentId(String practiceAppointmentId) {
        practiceClassMapper.deletePracticeClassByPracticeAppointmentId(practiceAppointmentId);
    }

    /**
     * 通过实践预约ids删除对应的内容
     * */
    @Override
    public int deletePracticeClassByPracticeAppointmentIds(String[] practiceAppointmentIds) {
        return practiceClassMapper.deletePracticeClassByPracticeAppointmentIds(practiceAppointmentIds);
    }
}
